﻿using System;
using System.Collections.Generic;
using FineUI;
using FTchina.Models;
using PWMIS.DataMap.Entity;
using FTchina.Library;
using System.Text;
using FTchina.Logic;
/****      
 * 
 * SelectorType=Mulit 多选人员
 * SelectorType=dpt 单选部门
 * SelectorType=Single单选人员
 * FileName=xxx 保存序列化文件的路径
 * 获取 List<UserList> list = UserListLogic.ReadListFromCookie("userSelector");
 * 
 * 调用 页面 if (!IsPostBack) 下添加
 * ViewState["FileName"] = this.GetHashCode().ToString();//获取
 * Button1.OnClientClick = Window1.GetShowReference("/ui/UserSelector.aspx?SelectorType=Mulit&FileName=" + (string)ViewState["FileName"]);
**/
public partial class ui_UserSelector : BasePage
{
    string SelectorType = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        SelectorType = RequestHelper.SafeRequest(Request["SelectorType"]);
        if (!IsPostBack)
        {

            AddRootTree();
        }
    }


    protected void btnSaveContinue_Click(object sender, EventArgs e)
    {
        // 1. 这里放置保存窗体中数据的逻辑
        SetList();

        // 2. 关闭本窗体，然后回发父窗体
        PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
    }
    /// <summary>
    /// 添加根项
    /// </summary>
    private void AddRootTree()
    {
        UserList ul = new UserList();
        OQLCompare cmp = new OQLCompare(ul);
        OQLCompare cmpresult = (cmp.Comparer(ul.ParentID, OQLCompare.CompareType.IS, "null"));
        OQL q = new OQL(ul);
        q.Select().Where(cmpresult).OrderBy(ul.Paixu, "ASC").OrderBy(ul.UName, "ASC");
        List<UserList> UList = EntityQuery<UserList>.QueryList(q, ul);
        foreach (UserList item in UList)
        {
            TreeNode node = new TreeNode();
            node.Text = item.UName;
            node.NodeID = item.Id.ToString();
            node.Leaf = false;
            node.EnableCheckBox = true;
            Tree1.Nodes.Add(node);
        }
    }
    /// <summary>
    /// 展开树
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Tree1_NodeExpand(object sender, FineUI.TreeExpandEventArgs e)
    {
        DynamicAppendNode(e.Node);
    }


    /// <summary>
    /// 动态展开根项
    /// </summary>
    /// <param name="parentNode"></param>
    private void DynamicAppendNode(TreeNode parentNode)
    {

        parentNode.Expanded = true;

        UserList ul = new UserList();
        OQLCompare cmp = new OQLCompare(ul);
        OQLCompare cmpresult = (cmp.Comparer(ul.ParentID, OQLCompare.CompareType.Equal, parentNode.NodeID));
        if (SelectorType == "dpt")
        {//部门模式，过滤
            cmpresult = cmpresult & cmp.Comparer(ul.IsDpt, OQLCompare.CompareType.Equal, "1");
        }
        OQL q = new OQL(ul);
        q.Select().Where(cmpresult).OrderBy(ul.Paixu, "ASC").OrderBy(ul.UName,"ASC");
        List<UserList> UList = EntityQuery<UserList>.QueryList(q, ul);

        foreach (UserList item in UList)
        {
            TreeNode node = new TreeNode();
            node.Text = item.UName;
            node.NodeID = item.Id.ToString();


            OQL qleave = new OQL(ul);
            qleave.Select().Where(cmp.Comparer(ul.ParentID, OQLCompare.CompareType.Equal, item.Id));
            List<UserList> UListLeave = EntityQuery<UserList>.QueryList(qleave, ul);
            node.Leaf = UListLeave.Count == 0;//是否子节点
            node.AutoPostBack = true;
            node.CommandArgument = item.UID;

            node.ToolTip = item.UName + item.UID;
            if (SelectorType == "Mulit" && item.IsDpt == "1")
            {
                node.EnableCheckBox = false;
            }
            else if (SelectorType == "Single" && item.IsDpt == "1")
            {
                node.EnableCheckBox = false;
            }
            else
            {
                node.EnableCheckBox = true;
            }


            parentNode.Nodes.Add(node);


        }
    }



    protected void Tree1_NodeCheck(object sender, FineUI.TreeCheckEventArgs e)
    {
        SetList();

    }

    private void SetList()
    {
        List<UserList> list = new List<UserList>();
        TreeNode[] nodes = Tree1.GetCheckedNodes();
        if (nodes.Length > 0)
        {
            if (SelectorType != "Mulit" & nodes.Length > 1)
            {//非多选模式只能选一个
                Alert.Show("您只能选择一个项");
                return;
            }
            else
            {

                StringBuilder sb = new StringBuilder();
                foreach (TreeNode node in nodes)
                {
                    UserList ul = new UserList();
                    ul.UName = node.Text;
                    ul.UID = node.CommandArgument;
                    ul.Id = Convert.ToInt32(node.NodeID);
                    list.Add(ul);

                    sb.AppendFormat("{0},", node.Text);

                }
                labResult.Text = "已选择：" + sb.ToString().TrimEnd(',');
            }
        }
        else
        {
            labResult.Text = "尚未选择";
        }
        UserListLogic.SaveList2Binary(list, Request["FileName"].ToString());//保存选择结果
    }
}
